# -*- coding: utf-8 -*-
# Pure SQL (ADBAPI) insertion


from twisted.enterprise import adbapi
from datetime import datetime

class RawDB(object):
    
    def insertEntry(self, values):
        values.update(
            {'date': datetime.strftime('%Y-%m-%d', values['time']),
             'time': datetime.strftime('%H:%M:%S', values['time'])})
        
        self.pool.runOperation(
            "INSERT INTO grdlog_event_current"
            " (date,time,host,program,pid,severity,facility,message) "
            "VALUES"
            " (:date,:time,:host,:program,:pid,:severity,:facility,:message)",
            values)
    
    def rotate(self, number):
        self.pool.runOperation(
            "SELECT rotate_event_table(?)",
            datetime.now().strftime("%Y%m%d%H%M"))
    
    def __init__(self, dsn):
        self.pool = adbapi.ConnectionPool('psycopg2', **dsn)
